function findById() {
    let id = sessionStorage.getItem("userId");
    console.log(id);
    let res = myAjax("/back/user/findById", {id: id});
    if (res.data != undefined) {
        setUserData(res.data);
    }

}

function setUserData(data) {

    layui.use(['form'],
        function () {
            var form = layui.form;

            $("#username").val(data.username);
            $("#phone").val(data.phone);
            $("#L_email").val(data.email);
            // $("#hobby").val(data.username);
            let hobby = data.hobby;// c++,c#
            let hobbys = hobby.split(",");
            // $("#enable").val(data.username);
            $('input[name="enable"][value="' + data.enable + '"]').prop('checked', 'checked');
            $("input[type=checkbox][name=hobby]").each(function () {
                for (let i = 0; i < hobbys.length; i++) {
                    if (hobbys[i] == $(this).attr("title")) {
                        $(this).attr("checked", true);
                    }
                }
                // if ($(this).is(":checked")) {
                //     hobby.push($(this).attr("title"));
                // }
                //     //jquery得到对象的属性值，用attr，如果对属性进行赋值也用attr('属性',‘值’)
                // hobby.push($(this).attr("title"));
            });
            $("#sex").val(data.sex);
            $("#birthday").val(data.birthday);
            $("#remark").val(data.remark);
            $("#userId").val(data.id);
            form.render();
        });
}

layui.use(['laydate', 'form', 'layer'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'//强制弹出日期选择框
        });
        findById();
        form.on('submit(edit)',
            function (data) {
                data = data.field;
                let hobby = [];
                $("input[type=checkbox][name=hobby]:checked").each(function () {
                    hobby.push($(this).attr("title"));
                });
                data.hobby = hobby.toLocaleString();
                // data.id = sessionStorage.getItem("userId");
                console.log(data);
                let res = myAjax("/back/user/update", data, "post");
                if (res.count > 0) {
                    //关闭当前frame
                    xadmin.close();

                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                } else {
                    layer.alert("增加失败");
                }

                return false;
            });
    });